package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"repo-man/models"
)

type RepoController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (r *RepoController) Add() {

	name := r.GetString("name")
	address := r.GetString("address")
	status := r.GetString("status")
	repo := models.Repo{
		Name:    name,
		Address: address,
		Status:  status,
	}
	if name == "" || address == "" || status == "" {
		r.Data["json"] = Res{
			Code: 0,
			Msg:  "参数不能为空",
		}
		r.ServeJSON()
		return
	}
	err := models.DB.Create(&repo).Error
	if err != nil {
		r.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		r.ServeJSON()
		return
	} else {
		r.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
		}
		r.ServeJSON()
	}
}
